# start from the 2009 code, as the scale linking is anchored to the 2009 estimates
source("3-d_UTAES_IRT_2009.R")
source("3-d_UTAES_IRT_2003.R")
source("3-d_UTAES_IRT_2005.R")
source("3-d_UTAES_IRT_2012.R")
source("3-d_UTAES_IRT_2014.R")
source("3-d_UTAES_IRT_2017.R")

v.year <- rep(NA, 100)
v.party <- rep(NA, 100)
v.ideology.mean <- rep(NA, 100)
v.ideology.lower <- rep(NA, 100)
v.ideology.upper <- rep(NA, 100)

### 2003
table(elected2003$partycod)
tapply(elected2003$Ideology, elected2003$partycod, mean)
##
i <- 1
v.year[i] <- "2003"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2003, partycod == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2003"
v.party[i] <- "DPJ_HOR"
tmp <- subset(elected2003, partycod == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2003"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2003, partycod == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2003"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2003, partycod == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2003"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2003, partycod == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2003"
v.party[i] <- "Conservative_HOR"
tmp <- subset(elected2003, partycod == 6)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

### 2005
table(elected2005$partycod)
tapply(elected2005$Ideology, elected2005$partycod, mean)
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2005, partycod == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "DPJ_HOR"
tmp <- subset(elected2005, partycod == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2005, partycod == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2005, partycod == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2005, partycod == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2005"
v.party[i] <- "PNP_HOR"
tmp <- subset(elected2005, partycod == 6)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

### 2009
table(elected2009$PARTY)
tapply(elected2009$Ideology, elected2009$PARTY, mean)
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2009, PARTY == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "DPJ_HOR"
tmp <- subset(elected2009, PARTY == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2009, PARTY == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2009, PARTY == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2009, PARTY == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "PNP_HOR"
tmp <- subset(elected2009, PARTY == 6)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2009"
v.party[i] <- "Your Party_HOR"
tmp <- subset(elected2009, PARTY == 7)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

### 2012
table(elected2012$PARTY)
tapply(elected2012$Ideology, elected2012$PARTY, mean)
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "DPJ_HOR"
tmp <- subset(elected2012, PARTY == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2012, PARTY == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "TPJ_HOR"
tmp <- subset(elected2012, PARTY == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2012, PARTY == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "JRP_HOR"
tmp <- subset(elected2012, PARTY == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2012, PARTY == 6)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "Your Party_HOR"
tmp <- subset(elected2012, PARTY == 7)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2012"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2012, PARTY == 8)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

### 2014
table(elected2014$PARTY)
tapply(elected2014$Ideology, elected2014$PARTY, mean)
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2014, PARTY == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "DPJ_HOR"
tmp <- subset(elected2014, PARTY == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "JRP_HOR"
tmp <- subset(elected2014, PARTY == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2014, PARTY == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "PJK_HOR"
tmp <- subset(elected2014, PARTY == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2014, PARTY == 6)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "TPJ_HOR"
tmp <- subset(elected2014, PARTY == 7)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2014"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2014, PARTY == 8)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

### 2017
table(elected2017$PARTY)
tapply(elected2017$Ideology, elected2017$PARTY, mean)
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "LDP_HOR"
tmp <- subset(elected2017, PARTY == 1)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "Komeito_HOR"
tmp <- subset(elected2017, PARTY == 2)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "JCP_HOR"
tmp <- subset(elected2017, PARTY == 3)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "JRP_HOR"
tmp <- subset(elected2017, PARTY == 4)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "JSP_HOR"
tmp <- subset(elected2017, PARTY == 5)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "PH_HOR"
tmp <- subset(elected2017, PARTY == 7)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]
##
i <- i + 1
v.year[i] <- "2017"
v.party[i] <- "CDP_HOR"
tmp <- subset(elected2017, PARTY == 8)$Ideology
tmp <- -tmp
v.ideology.mean[i] <- mean(tmp)
v.ideology.lower[i] <- t.test(tmp)$conf.int[1]
v.ideology.upper[i] <- t.test(tmp)$conf.int[2]

df.tmp <- cbind(v.year, v.party, v.ideology.mean, v.ideology.lower, v.ideology.upper)
colnames(df.tmp) <- c("year", "party", "ideology.mean", "ideology.lower", "ideology.upper")
df.tmp <- df.tmp[complete.cases(df.tmp), ]
write.csv(df.tmp, "3-c_UTAES-based_scores.csv")